package com.creditrank.indexer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Arrays;

/*
 * Author : Krishna Nagarjun Reddy
 */

public class IndexCreate
{
	  public static IndexReader reader;		  // existing index
	  public static IndexWriter writer;
	  static File index,root;
	 // public IndexCreate() throws Exception, LockObtainFailedException, IOException
	  //{
		  
		  
		  
	  //}
	  //This method should be called to index a document
	  public static void index(String url,int i,String Content)
	  {
		  
		 try
		 {
			 //Taking the location to store the index
			 index=new File("../../index1");
			if(i==1)
			{
				writer = new IndexWriter(FSDirectory.open(index), new StandardAnalyzer(Version.LUCENE_CURRENT), true, new IndexWriter.MaxFieldLength(1000000));
				//System.out.println(" inside i=1 \n");
			}
			else
			{
			writer = new IndexWriter(FSDirectory.open(index), new StandardAnalyzer(Version.LUCENE_CURRENT), false, new IndexWriter.MaxFieldLength(1000000));
			//System.out.println(" inside i!=1 \n");
			}
			 root=new File("1.html");
			 //Date start = new Date();
			 
			 //adding new documents to index
			// System.out.println(root.toString());
			 //indexDocs(url,Content);	
			Document doc = HTMLdoc.Document(url,Content);
		  	
	       		 System.out.println("adding " + doc.get("path"));
	       	 	writer.addDocument(doc);
			 writer.optimize();
			 writer.close();
			 //root.delete();
			
		 }
		 catch (Exception e) 
		 {
	         e.printStackTrace();
		 }
		  
		  
	  }
	   
	  
}

